define(function(require, exports, module) {
	var gIsIE = document.all;
	module.exports = new (function(){
		this.isIE = !!document.all;
		
		this.init = function(){
			this.iframeEl = this.el.querySelector('iframe');
			this.iframeWin = this.iframeEl.contentWindow;
			this.sourceEditor = this.el.querySelector('textarea');
			this.body = this.iframeWin.document.body;
			
			bf.dom.getParentEl('.fontfamilyChoser').onclick = function(e){
				var el = e.target;
				if(el.tagName != 'A')return;
				
				this.format('fontname', el.innerHTML);
			}.bind(this);

			bf.dom.getParentEl('.fontsizeChoser').addEventListener('click', function(e){	
				var el = e.target;
				if(el.tagName != 'A')return;
				
				var holder = e.currentTarget, els = holder.children;
				
				for(var i = 0, j = els.length; i < j; i++){
					if(el == els[i])break;
				}
				this.format('fontsize', i);
			}.bind(this));
			
			//document.querySelector('.colorPickerBtn').innerHTML += createColorPickerHTML();
		
			// 这个方法只能写在 onload 事件 不写 onload 里还不执行
			var doc = this.iframeWin.document;
			window.setTimeout(function(){
				bf.dom.getParentEl('.editorBody iframe').contentWindow.document.designMode='on';
//				doc.designMode = "on";
			}, 300);	
			
			window.addEventListener('load', function(){
				var doc = this.iframeWin.document;
				doc.designMode = "on";
				
				if(!this.isIE)doc.execCommand("useCSS", false, true);
			}.bind(this));
			 
			
		}
		
		this.format = function (type, para) {
			this.iframeWin.focus();
			
			if (!para){
				if (gIsIE)
					this.iframeWin.document.execCommand(type);
				else 
					this.iframeWin.document.execCommand(type, false, false);
			}else 
				this.iframeWin.document.execCommand(type, false, para);
			
			this.iframeWin.focus();
		}
		
		this.createLink = function () {
			var sURL = window.prompt("请输入网站绝对地址（相对地址请使用图片上传）:", "http://");
			if (sURL && (sURL != "http://"))this.format("CreateLink", sURL);
		}
		
		this.createImg = function () {
			var sPhoto = prompt("请输入图片位置:", "http://");
			if (sPhoto && (sPhoto != "http://"))this.format("InsertImage", sPhoto);
		}
		
		this.setEditable = function(isEditable){
			// 异常工具条
			if(!isEditable){
				var toolbar = this.el.$('.toolbar')(true);
				this.el.removeChild(toolbar, this.el);
				
				this.iframeEl.parentNode.style.borderTopWidth = '1px';
			}
			
			setTimeout(function(){
				bf.dom.getParentEl('.editorBody iframe').contentWindow.document.designMode = (isEditable) ? 'on' : 'off';
			}, 300);
		} 
		
		this.setValue = function(v){
			var iframeEl = this.iframeEl;
			setTimeout(function(){
				iframeEl.contentWindow.document.body.innerHTML = v;
			}, 400);
		}
		
		this.getValue = function(){
			return this.iframeEl.contentWindow.document.body.innerHTML;
		}
		this.setMode = function (bStatus) {
			var divEditor =  this.iframeEl.parentNode;
			
			if (bStatus) {
				this.iframeEl.style.height = divEditor.style.height = "0";

				this.sourceEditor.style.display = "";
				this.sourceEditor.value = this.iframeWin.document.body.innerHTML;
			} else {
				this.sourceEditor.style.display = "none";
				
				this.iframeEl.style.height = divEditor.style.height = "286px";
				this.iframeWin.document.body.innerHTML = this.sourceEditor.value;
			}
		}
		
		// by pageEditor
		this.openUploader = function(){
			// 获取文件夹路径
			var folder = location.search.match(/url=([^&]*)/).pop();
			var serviceUrl = imgsServiceUrl = '?action=getImgList&folder=' + folder;
			var uploadDialog = Object.create(require('bigfoot/widget/upload/panel'));
			uploadDialog.serviceUrl	= imgsServiceUrl;
			uploadDialog.delUrl		= '?action=delPic&file=' + folder;
			uploadDialog.createUrl	= '?action=upload&folder=' + folder;
			uploadDialog.urlPrefix	= '../../' + folder;
			
			uploadDialog.init(document.querySelector('.uploadPanel_Tpl'));
			uploadDialog.loadImgs(imgsServiceUrl);
		}
		
		this.openUploaderByGallery = function(){
			var id = location.search.match(/id=(\d+)/);
			if(!id){
				alert('请先新建记录，再上传图片');
				return;
			}
			id = id.pop();
			
			var imgsServiceUrl = serviceUrl + '/' + id + '/img';

			var uploadDialog = Object.create(require('bigfoot/widget/upload/panel'));
			uploadDialog.serviceUrl	= imgsServiceUrl;
			uploadDialog.delUrl		= serviceUrl + '/img';
			uploadDialog.createUrl	= serviceUrl + '/img?parentId=' + id;
			uploadDialog.setCoverUrl= serviceUrl + '/' + id;
			uploadDialog.urlPrefix	= '../../public/gallery';
			
			uploadDialog.init(document.querySelector('.uploadPanel_Tpl'));
			uploadDialog.loadImgs(imgsServiceUrl);

		}
		
		this.openUploader = function(){
			// 获取文件夹路径
			var folder = location.search.match(/url=([^&]*)/).pop();
			var serviceUrl = imgsServiceUrl = '?action=getImgList&folder=' + folder;
			var uploadDialog = Object.create(require('bigfoot/widget/upload/panel'));
			uploadDialog.serviceUrl	= imgsServiceUrl;
			uploadDialog.delUrl		= '?action=delPic&file=' + folder;
			uploadDialog.createUrl	= '?action=upload&folder=' + folder;
			uploadDialog.urlPrefix	= '../../' + folder;
			
			uploadDialog.init(document.querySelector('.uploadPanel_Tpl'));
			uploadDialog.loadImgs(imgsServiceUrl);
		}
		
		this.insert2article = function(img){
			this.format("InsertImage", img);
		}
	});

	function createColorPickerHTML(){
		// 定义变量
		var cl = [ '00', '33', '66', '99', 'CC', 'FF' ], a, b, c, d, e, f, i, j, k, T;
		// 创建head
		var h = '<div class="colorPicker">';
		h += '<div class="colorhead"><span class="colortitle">颜色选择</span></div>';
		// 创建body [6 x 6的色盘]
		h += '<div class="colorbody"><table cellspacing="0" cellpadding="0"><tr>';
		
		for (i = 0; i < 6; ++i) {
			h += '<td><table class="colorpanel" cellspacing="0" cellpadding="0">';
			for (j = 0, a = cl[i]; j < 6; ++j) {
				h += '<tr>';
				for (k = 0, c = cl[j]; k < 6; ++k) {
					b = cl[k];
					e = k == 5 && i != 2 && i != 5 ? ';border-right:none;' : '';
					f = j == 5 && i < 3 ? ';border-bottom:none' : '';
					d = '#' + a + b + c;
					T = document.all ? '&nbsp;' : ''
						h += '<td unselectable="on" style="background: ' + d + e
						+ f + '" title="' + d + '">' + T + '</td>'; /* 切记设置unselectable='on' */
				}
				h += '</tr>';
			}
			h += '</table></td>';
			if (cl[i] == '66')
				h += '</tr><tr>';
		}
		h += '</tr></table></div></div>';
		
		return h;
	}
});